home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / util / gnu / emacs_src_18_58.lha / emacs-18.58 / lisp / telnet.el < prev    next >
Text File  |  1992-02-21  |  8KB  |  218 lines

  1. ;; Copyright (C) 1985 Free Software Foundation
  2.  
  3. ;; This file is part of GNU Emacs.
  4.  
  5. ;; GNU Emacs is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation; either version 1, or (at your option)
  8. ;; any later version.
  9.  
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. ;; GNU General Public License for more details.
  14.  
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  17. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19.  
  20. ;; Author William F. Schelter
  21.  
  22. ;;to do fix software types for lispm:
  23. ;;to eval current expression.  Also to try to send escape keys correctly.
  24. ;;essentially we'll want the rubout-handler off.
  25.  
  26. (defvar telnet-new-line "\r")
  27. (defvar telnet-mode-map nil)
  28. (defvar telnet-prompt-pattern "^[^#$%>]*[#$%>] *")
  29. (defvar telnet-interrupt-string "\^c" "String sent by C-c.")
  30. (defvar telnet-count 0)
  31. (defvar telnet-replace-c-g nil)
  32. (defvar telnet-remote-echoes nil)
  33.  
  34. (defun telnet-interrupt-subjob ()
  35.   (interactive)
  36.   "Interrupt the program running through telnet on the remote host."
  37.   (send-string nil telnet-interrupt-string))
  38.  
  39. (defun telnet-c-z ()
  40.   (interactive)
  41.   (send-string nil ""))
  42.  
  43. (defun send-process-next-char ()
  44.   (interactive)
  45.   (send-string nil
  46.            (char-to-string
  47.         (let ((inhibit-quit t))
  48.           (prog1 (read-char)
  49.             (setq quit-flag nil))))))
  50.  
  51. (setq telnet-mode-map (make-sparse-keymap))
  52.  
  53. (progn
  54.   (define-key telnet-mode-map "\C-m" 'telnet-send-input)
  55.   (define-key telnet-mode-map "\C-j" 'telnet-send-input)
  56.   (define-key telnet-mode-map "\C-c\C-d" 'shell-send-eof)
  57.   (define-key telnet-mode-map "\C-c\C-q" 'send-process-next-char)
  58.   (define-key telnet-mode-map "\C-c\C-c" 'telnet-interrupt-subjob) 
  59.   (define-key telnet-mode-map "\C-c\C-z" 'telnet-c-z)
  60.   (define-key telnet-mode-map "\C-c\C-u" 'kill-shell-input)
  61.   (define-key telnet-mode-map "\C-c\C-w" 'backward-kill-word)
  62.   (define-key telnet-mode-map "\C-c\C-o" 'kill-output-from-shell)
  63.   (define-key telnet-mode-map "\C-c\C-r" 'show-output-from-shell))
  64.  
  65. ;;maybe should have a flag for when have found type
  66. (defun telnet-check-software-type-initialize (string)
  67.   "Tries to put correct initializations in.  Needs work."
  68.   (cond ((string-match "unix" string)
  69.      (setq telnet-prompt-pattern shell-prompt-pattern)
  70.      (setq telnet-new-line "\n"))
  71.     ((string-match "tops-20" string) ;;maybe add telnet-replace-c-g
  72.      (setq telnet-prompt-pattern  "[@>]*"))
  73.     ((string-match "its" string)
  74.      (setq telnet-prompt-pattern  "^[^*>]*[*>] *"))
  75.     ((string-match "explorer" string)  ;;explorer telnet needs work
  76.      (setq telnet-replace-c-g ?\n))
  77.     ))
  78.  
  79. (defun telnet-initial-filter (proc string)
  80.   ;For reading up to and including password; also will get machine type.
  81.   (cond ((string-match "No such host" string)
  82.      (kill-buffer (process-buffer proc))
  83.      (error "No such host."))
  84.     ((string-match "passw" string)
  85.      (telnet-filter proc string)
  86.      (let* ((echo-keystrokes 0)
  87.         (password (read-password)))
  88.        (setq telnet-count 0)
  89.        (send-string proc (concat password  telnet-new-line))))
  90.     (t (telnet-check-software-type-initialize string)
  91.        (telnet-filter proc string)
  92.        (cond ((> telnet-count 4)
  93.           (set-process-filter proc 'telnet-filter))
  94.          (t (setq telnet-count (1+ telnet-count)))))))
  95.  
  96. (defun telnet-filter (proc string)
  97.   (save-excursion
  98.     (set-buffer (process-buffer proc))
  99.     (goto-char (point-max))
  100.     (let ((now (point)))
  101.       (insert string)
  102.       (subst-char-in-region now (point) ?\^m ?\ )
  103.       (and telnet-replace-c-g
  104.        (subst-char-in-region now (point) ?\^g telnet-replace-c-g)))
  105.     (if (process-mark proc)
  106.     (set-marker (process-mark proc) (point)))
  107.     (if (and (integer-or-marker-p last-input-start)
  108.          (marker-position last-input-start)
  109.          telnet-remote-echoes)
  110.     (delete-region last-input-start last-input-end)))
  111.   (if (eq (process-buffer proc)
  112.       (current-buffer))
  113.       (goto-char (point-max))))
  114.  
  115. (defun delete-char-or-send-eof (arg killp)
  116.   "At end of buffer, send eof to subshell.  Otherwise delete character."
  117.   (interactive "p\nP")
  118.   (if (and (eobp) (not killp))
  119.       (process-send-eof)
  120.     (delete-char arg killp)))
  121.  
  122. (defun telnet-send-input ()
  123.   "Send input to remote host
  124. At end of buffer, sends all text after last output
  125. as input to the telnet, including a telnet-new-line inserted at the end.
  126. Not at end, copies current line to the end of the buffer and sends it,
  127. after first attempting to discard any prompt at the beginning of the line
  128. by matching the regexp that is the value of telnet-prompt-pattern if possible."
  129.  (interactive)
  130.  (let (copied)
  131.   (end-of-line)
  132.   (if (eobp)
  133.       (progn
  134.     (move-marker last-input-start
  135.              (process-mark (get-buffer-process (current-buffer))))
  136.     (move-marker last-input-end (point)))
  137.     (beginning-of-line)
  138.     (re-search-forward telnet-prompt-pattern nil t)
  139.     (let ((copy (buffer-substring (point)
  140.                   (progn (forward-line 1) (point)))))
  141.       (goto-char (point-max))
  142.       (move-marker last-input-start (point))
  143.       (insert copy) (setq copied t)
  144.       (move-marker last-input-end (point))))
  145.   (save-excursion
  146.     (goto-char last-input-start)
  147.     (let ((process (get-buffer-process (current-buffer))))
  148.       (send-region process last-input-start last-input-end)
  149.       (if (not copied) (send-string process telnet-new-line))
  150.       (set-marker (process-mark process) (point))))))
  151.  
  152. (defun telnet (arg)
  153.   "Open a network login connection to host named HOST (a string).
  154. Communication with HOST is recorded in a buffer *HOST-telnet*.
  155. Normally input is edited in Emacs and sent a line at a time."
  156.   (interactive "sOpen telnet connection to host: ")
  157.   (require 'shell)
  158.   (let ((name (concat arg "-telnet" )))
  159.     (switch-to-buffer (make-shell name "telnet"))
  160.     (set-process-filter (get-process name) 'telnet-initial-filter)
  161.     (erase-buffer)
  162.     (send-string  name (concat "open " arg "\n"))
  163.     (telnet-mode)
  164.     (setq telnet-count -16)))
  165.  
  166. (defun read-password ()
  167.   (let ((answ "") tem)
  168.     (while (not (or (= (setq tem (read-char)) ?\^m)
  169.             (= tem ?\n)))
  170.       (setq answ (concat answ (char-to-string tem)))
  171.       (setq quit-flag nil))
  172.     answ))
  173.  
  174. (defun telnet-mode ()
  175.   "This mode is for use during telnet from a buffer to another
  176. host. It has most of the same commands as shell mode.
  177. There is a variable `telnet-interrupt-string' which is the character
  178. sent to try to stop execution of a job on the remote host.
  179. Data is sent to the remote host when `return' is typed.
  180. Thus if you may need to edit the data before sending you
  181. should use c-n to move down a line.  Then you can return
  182. to alter a previous line.  Of course you should not use this
  183. mode of telnet if you want to run emacs like programs on the
  184. remote host (at least not yet!).
  185.  
  186. The following commands imitate the usual Unix interrupt and
  187. editing control characters:
  188. \\{telnet-mode-map}
  189.  
  190. Bugs:
  191. --Replace  by a space, really should remove.
  192. --For Unix interacts poorly with tcsh although csh,sh,ksh are ok."
  193.   (interactive)
  194.   (kill-all-local-variables)
  195.   (setq major-mode 'telnet-mode)
  196.   (setq mode-name "Telnet")
  197.   (setq mode-line-process '(": %s"))
  198.   (make-local-variable 'last-input-start)
  199.   (use-local-map telnet-mode-map)
  200.   (let ((tem telnet-prompt-pattern))
  201.     (make-local-variable 'telnet-prompt-pattern)
  202.     (setq telnet-prompt-pattern tem))
  203.   (make-local-variable 'telnet-interrupt-string)
  204.   (setq telnet-interrupt-string "")
  205.   (make-local-variable 'telnet-new-line)
  206.   (setq telnet-new-line "\r")
  207.   (make-local-variable 'last-input-start)
  208.   (setq last-input-start (make-marker))
  209.   (make-local-variable 'last-input-end)
  210.   (setq last-input-end (make-marker))
  211.   (make-local-variable 'telnet-remote-echoes)
  212.   (setq telnet-remote-echoes t)
  213.   (make-local-variable 'telnet-replace-c-g)
  214.   (setq telnet-replace-c-g nil))
  215.  
  216.  
  217.  
  218.